/*
 *  总结：
 *  牵扯到数据结构，能使用map不用数组
 *  如果对数据结构存储的唯一性有要求，使用set
 *  放弃使用object和数组
 */
{
    let list = new Set();
    list.add(5);
    list.add(7);
    console.log(list.size)
}

{
    let arr = [1, 2, 3, 4, 5];
    let list = new Set(arr);
    console.log('size', list.size);
}

{
    //  set()数据类型的元素必须是唯一的
    let list = new Set();
    list.add(1);
    list.add(2);
    list.add(1);
    console.log('list', list)

    //  数组去重，set()转换过程中不会转换数据类型
    let arr = [1, 2, 3, 1, '2'];
    let list2 = new Set(arr);
    console.log('unique', list2)
}

{
    //  判断时候有某个元素
    let arr = ['add', 'delete', 'clear', 'has'];
    let list = new Set(arr);
    console.log('has', list.has('add'))
    console.log('delete', list.delete('add'), list);
    list.clear();
    console.log('list', list)
}

{
    //  Set()遍历
    let arr = ['add', 'delete', 'clear', 'has'];
    let list = new Set(arr);

    for (let key of list.keys()) {
        console.log('keys', key);
    }

    for (let value of list.values()) {
        console.log('values', value);
    }

    for (let [key, value] of list.entries()) {
        console.log('entries', key, value);
    }

    list.forEach(item => console.log(item));
}

//  WeakSet()只支持对象类型
//  WeakSet()的值都是弱引用，只引用地址，不检测垃圾回收
{
    let weakList = new WeakSet();
    let arg = {};
    weakList.add(arg);
    console.log('weakList', weakList)
}

//  map()添加元素用set
{
    let map = new Map();
    let arr = ['123'];
    map.set(arr, 456);
    console.log('map', map, map.get(arr));
}

{
    let map = new Map([['a', 123], ['b', 456]]);
    console.log('map args', map);

    //  size属性值
    console.log('size', map.size);

    //  删除
    console.log('delete', map.delete('a'), map);

    //  清空
    console.log('clear', map.clear(), map);
}

// WeakMap和WeakSet类似
{
    let weakmap = new WeakMap();
    let o = {}
    weakmap.set(o, 123);
    console.log(weakmap.get(o))
}

//  数据结构横向对比，增删改查
{
    //  map和array对比
    let map = new Map();
    let array = [];
    //  增
    map.set('t', 1);
    array.push({ t: 1 });
    console.info('map-array', map, array);

    // 删
    map.delete('t');
    let index = array.findIndex(item => item.t);
    array.splice(index, 1);
    console.info('map-array-delete', map, array);

    // 改
    map.set('t', 2);
    array.forEach(item => item.t ? item.t = 2 : '');
    console.info('map-array-modify', map, array);

    //   查
    let map_exist = map.has('t');
    let array_exist = array.find(item => item.t);
    console.info('map-array', map_exist, array_exist);
}

{
    //  set和array对比
    let set = new Set();
    let array = [];

    //  增
    set.add({ t: 1 });
    array.push({ t: 1 });
    console.info('set-array', set, array);

    //  删
    set.forEach(item => item.t ? set.delete(item) : '');
    let index = array.findIndex(item => item.t);
    array.splice(index, 1);
    console.info('set-array-delete', set, array);

    //  改
    set.forEach(item => item.t ? item.t = 2 : '');
    array.forEach(item => item.t ? item.t = 2 : '');
    console.info('set-array-modify', set_exist, array);

    //  查
    let set_exist = set.has({ t: 1 });
    let array_exist = array.find(item => item.t);
    console.info('set-array', set_exist, array_exist);
}

{
    //  map,set,object对比
    let item = { t: 1 }
    let map = new Map();
    let set = new Set();
    let obj = {};

    //  增
    map.set('t', 1);
    set.add(item);
    obj['t'] = 1;
    console.info('map-set-obj', map, set, obj);

    //  删
    map.delete('t');
    set.delete(item);
    delete obj['t'];
    console.info('map-set-obj-delete', map, set, obj);

    //  改
    map.set('t', 2);
    item.t = 2;
    obj['t'] = 2;
    console.info('map-set-obj', map, set, obj);

    //  查
    console.info({
        map_exist: map.has('t'),
        set_exist: set.has(item),
        obj_exist: 't' in obj
    });
}

// class App {
//     constructor() {

//     }
//     self () {
//         console.log(this);
//         this.new();
//     }
//     new () {
//         console.log('执行方法')
//     }
// }

// let app = new App();
// app.self();